/*****************************************************************
**                                                             	**
**  Copyright (C) 2004 Amlogic,Inc.                            	**
**  All rights reserved                                        	**
**        Filename : aw_gui_app.c /Project:AVOS customer demo	** 
**        Revision : 1.0                                       	**
**                                                             	**
*****************************************************************/
#include "includes.h"
#include <aw_playlist2.h>
#include "aw_windows.h"
#include "menu.h"
#include "aw_init_display.h"
#include "user.h"
#include "aw_gui_app.h"
#include "logo_dat.h" 
#include <app/avi/avi_sub.h>
#include "../user.h"
#include "aw_AVI_app.h"
#include "aw_VOB_app.h"
#include "aw_GPLAYER_app.h"
#include "gplayer_app.h"
#include "win/LogoEvent.h"
#include "filenav.h"
#include "fs_playlist.h"
#include "aw_playlist.h"
#include "AVTimer.h"
#include "NetmenuSetupEvent.h"
#include "aw_PIC_app.h"
#include "aw_sys_app.h"
#include "ctrlconvert.h"
#include "Chipsupport.h"
#include "volatile_read.h" 
#include "DeviceSelectEvent.h"
#include "chipsupport.h"
#include "bsp.h"

#ifdef SUPPORT_RM
#include "aw_RM_app.h"
#endif
#ifdef ENABLE_NETWORK
void Preview_browser_task_post(void);
#endif

#define GUI_NEED_PLAY_NOTHING   0
#define GUI_NEED_PLAY_AVI       1
#define GUI_NEED_PLAY_VOB       2
#define GUI_NEED_PLAY_GPLAYER   3
#define ALL_FILE			0x3ff
#define READ_MPEG_REG_ISA(reg)           (READ_VOLATILE_UINT32(reg)) 
#define WRITE_MPEG_REG_ISA(reg, val) *(volatile unsigned *) (reg) = (val)

PHWND file_list_control;
FILESETCB file_list_show_cb ;
FILESETCB file_list_hide_cb ;
AWMSG *AutoRunMsg;

AVTimer_t auto_play_timer = 0;
extern AVTimer_t sys_restart_time;
extern INT8U copy_del_flag;

#ifdef AML_DIR
extern char preview_item_name[256];
extern INT32U cur_level_index;
#endif
#if (defined AML_DIR)||(defined ENABLE_BLUETOOTH)
static INT8U check_app_flag = 0;
#endif

char loading_autorun_flag = 1;
int start_addr = 0 ;
int  AUTORUN_DEPTH  = 0xf00 ;   //dir search depth up to 15 level,default is 0x500(5 level)
INT8U preview_processing = 0;
INT32S roll_string_timeout = 0;
INT32U last_hotkey = UOP_NOP ;

#if HOTKEYENABLE
char hotkey_flag = 0 ;
#endif 

#ifdef ENABLE_BLUETOOTH
extern char gCurOnlyFileName[256] ;
#endif

#ifndef USE_NEW_SCREEN_SAVER 
AVTimer_t keytimer = 0;
#endif
static INT8U scrsaver_on = 0 ;
static INT8U scrsaver_time = 0 ;
INT8U gScreenSaveFlag = 0;

extern int MultiMenuToSingle,aw_title_mode;
extern int preview_video;
extern INT8U keep_current_index ;
extern INT8U  Music_Pic_mix_flag ;
extern INT8U ShowSixPicFlag;	
extern INT8U PlayBack_unMute_WaitFor ;
extern INT8U PlayBack_unMute_waitting ;
extern INT8U PlayBack_unMute_ShowFlag ;
extern INT8U isMute ;
extern INT8U CurrentOsdSize;
extern INT8U WinTypeChoice;
extern INT8U FileListOnShowFlag;
extern INT32U NODIR_FLAG;  //0 for with dir, 1 for playing music without dir, 2 for playing video without dir.
extern char autorun_audio_flag;
extern char autorun_video_flag;
extern char autorun_pic_flag;
extern char CurDriver[10];
extern char multi_vol_autorun ;
extern char EnableLogo;
extern unsigned char user_power_flag;
extern char *unsupport_item_name;
extern INT8U Music_Play_flag ;
extern INT8U Music_Play_Dlying ;
extern INT8U filelist_show_flag;
extern INT32U cur_filetype_flag;
extern char force_display_logo;

static INT8U mute_status = 0;
static INT32S  key_input_timeout=0;
static char *gui_play_filename = NULL ;
static unsigned load_videoapp_data[10];

unsigned char PC_Connect=0;

extern void PicParaTrans(void);

extern int check_media_file_exist(char* path_name ,unsigned media_type,int search_way);

extern avfs_device_driver hddisk_dev_start(void) ;

extern int RunGPlayer_loop(Playlist_t *playlist);

void ScrSaverSet(INT8U on) 
{
    scrsaver_on = on ;
}

void ScrSaverReset() 
{
    scrsaver_time = 0 ;
}

#ifdef ENABLE_WATCHDOG
#include <Drivers/watchdog/watchdog.h>

static void Reset_Watchdog()
{
    if(sys_restart_time)
   	{	       
   	AVTimerDelete(sys_restart_time);	      
	sys_restart_time= 0;	
	}
    _disable_dcache();
    OS_DISABLE_ALL();
    chip_power_on(1);
}
#endif

INT8U GetMuteStatus(void)
{
	return mute_status;
}

void SetMuteStatus(INT8U value)
{
	mute_status=value;
}


INT8U CloseMediaPlaybackApp(INT8U flag)
{
    INT8U my_app_id ;
     AWGetAppID(MEDIA_PIC, &my_app_id) ; 
    if (AWGetAppStatus(my_app_id) == APP_RUNNING){
		//send_picplayer_stop();
		//AWGUISendNMMessage(MEDIA_PIC, NM_END_OF_PLAY, (INT32U)flag);
		AWPICKeyDo(UOP_STOP) ;
	}
   AWGetAppID(MEDIA_AVI, &my_app_id) ; 
    if (AWGetAppStatus(my_app_id) == APP_RUNNING){
		//AWGUISendNMMessage(MEDIA_AVI, NM_END_OF_PLAY, (INT32U)flag);
        AWAVIQuitProcess();
    }
   AWGetAppID(MEDIA_GENERAL, &my_app_id) ; 
    if (AWGetAppStatus(my_app_id) == APP_RUNNING){
		//AWGUISendNMMessage(MEDIA_GENERAL, NM_END_OF_PLAY, (INT32U)flag);
         AWGPLAYERQuitProcess();
    }

    AWGetAppID(MEDIA_TXT, &my_app_id) ; 
    if (AWGetAppStatus(my_app_id) == APP_RUNNING){
		//AWGUISendNMMessage(MEDIA_TXT, NM_END_OF_PLAY, (INT32U)flag);
        //AWTXTQuitProcess(0) ;
        AWTXTKeyDo(UOP_STOP,0);
	}
    AWGetAppID(MEDIA_MPEG2, &my_app_id) ; 
    if (AWGetAppStatus(my_app_id) == APP_RUNNING){
		//AWGUISendNMMessage(MEDIA_MPEG2, NM_END_OF_PLAY, (INT32U)flag);
        AWVOBQuitProcess();	
    }
#ifdef SUPPORT_RM
    AWGetAppID(MEDIA_REAL, &my_app_id) ; 
    if (AWGetAppStatus(my_app_id) == APP_RUNNING){
		//AWGUISendNMMessage(MEDIA_MPEG2, NM_END_OF_PLAY, (INT32U)flag);
        AWRMQuitProcess();	
    }
#endif
    return 0;
}
#ifdef ENABLE_BLUETOOTH
INT8U Bt_CloseMediaPlaybackApp(INT8U flag)
{
    INT8U my_app_id ;
     AWGetAppID(MEDIA_PIC, &my_app_id) ; 
    if (AWGetAppStatus(my_app_id) == APP_RUNNING){
		//send_picplayer_stop();
		AWGUISendNMMessage(MEDIA_PIC, NM_END_OF_PLAY, (INT32U)flag);
		//AWPICKeyDo(UOP_STOP) ;
	}
   AWGetAppID(MEDIA_AVI, &my_app_id) ; 
    if (AWGetAppStatus(my_app_id) == APP_RUNNING){
		//AWGUISendNMMessage(MEDIA_AVI, NM_END_OF_PLAY, (INT32U)flag);
        AWAVIQuitProcess();
    }
   AWGetAppID(MEDIA_GENERAL, &my_app_id) ; 
    if (AWGetAppStatus(my_app_id) == APP_RUNNING){
		//AWGUISendNMMessage(MEDIA_GENERAL, NM_END_OF_PLAY, (INT32U)flag);
         AWGPLAYERQuitProcess();
    }

    AWGetAppID(MEDIA_TXT, &my_app_id) ; 
    if (AWGetAppStatus(my_app_id) == APP_RUNNING){
		AWGUISendNMMessage(MEDIA_TXT, NM_END_OF_PLAY, (INT32U)flag);
	}
    AWGetAppID(MEDIA_MPEG2, &my_app_id) ; 
    if (AWGetAppStatus(my_app_id) == APP_RUNNING){
		//AWGUISendNMMessage(MEDIA_MPEG2, NM_END_OF_PLAY, (INT32U)flag);
        AWVOBQuitProcess();	
    }
#ifdef SUPPORT_RM
    AWGetAppID(MEDIA_REAL, &my_app_id) ; 
    if (AWGetAppStatus(my_app_id) == APP_RUNNING){
		//AWGUISendNMMessage(MEDIA_MPEG2, NM_END_OF_PLAY, (INT32U)flag);
        AWRMQuitProcess();	
    }
#endif
    return 0;
}
#endif
/* The control that needs to be brought back to the front 
 * when playing is done */
void AWSetFileList(PHWND control, FILESETCB show_cb, FILESETCB hide_cb)
{
    file_list_control = control;
    file_list_show_cb = show_cb;
    file_list_hide_cb = hide_cb;
}

PHWND AWGetFileList(void)
{
    return file_list_control;
}

void AWShowFileList(INT8U flag)
{
    PHWND win_ctrl = AWGetFileList();
    PHWND top = AWGetTopWindow(win_ctrl);

    if(top) 
        AWShowWindow(top, 1);
    else if(win_ctrl)
        AWShowWindow(win_ctrl, 1);
    else{
	FileListOnShowFlag=0;
//	ReloadLogo();
    	GUIShowWindow(WinFileList);
    	}
    if(file_list_show_cb && flag) {
        file_list_show_cb();
    }
    
}

#ifdef ENABLE_BLUETOOTH
void AWShowPreWindow(INT8U flag)
{
    PHWND win_ctrl = AWGetFileList();
    PHWND top = AWGetTopWindow(win_ctrl);

    if(top) 
        AWShowWindow(top, 1);
    else if(win_ctrl)
        AWShowWindow(win_ctrl, 1);
    else{
	FileListOnShowFlag=0;
//	ReloadLogo();
    	GUIShowWindow(WinDeviceSelect);
    	}
    if(file_list_show_cb && flag) {
        file_list_show_cb();
    }
    
}
#endif
void AWHideFileList(void)
{
    if(file_list_hide_cb) {
        file_list_hide_cb() ;
    }
}


int AWLoadVideoApp(int media_type, CMDSETRESULTHANDLE callback, char *file_name)
{
	PCMDSET pCmd ;
    INT8U my_app_id ;

    /*
     * Sample Code:
     * 1. Start AVI application
     *      How:
     *          AWGetAppID(MEDIA_AVI, &my_app_id);   
     *          AWAppRunning(my_app_id);
     *    The AVI payback thread is running and waiting for CM_LOAD_APP command
     * 2. Send initial playback parameter to AVI 
     *      How:
     *      The command type of CM_LOAD_APP:
     *      parameter[0]:   file name address
     *      parameter[1]:   key table address
     *      parameter[2]:   logo data address
     *      parameter[3]:   logo data size
     *      parameter[4]:   user context, normally is application callback to
     *                      interrupt data reading operation
     *      Note:
     *      1) for encrypted AVI file, key table holds 128*16 bytes of keys;
     *         for non-encrypted AVI file, 0 must be put in the key table field
     *      2) When AVI playback quits, the logo will be displayed on the screen
     */
    
    pCmd = AWCommandSetMalloc();

    if(pCmd) {
        pCmd->command_num = 1;
        pCmd->curr_executed_cmd_num = 0;
        pCmd->commands[0] = CM_LOAD_APP;
        pCmd->commands_para[0] = (INT32U)load_videoapp_data;

        load_videoapp_data[0] = (unsigned)file_name;
        load_videoapp_data[1] = 0;
        load_videoapp_data[2] = (unsigned)Logo_keyboard;
        load_videoapp_data[3] = 0;
        load_videoapp_data[4] = 0;
        load_videoapp_data[5] = 0;
        if(preview_video)
        {
            preview_processing = 1;
            unsigned short start_x=336, start_y=89, width=360, height=240, preview_flag=1, audio_mute=1;
            unsigned char video_mode = DISPCTL_MODE_480I;  
            switch (CurrentOsdSize){
                case OsdSize480i:
                case OsdSize480iCVBS:
                    video_mode = DISPCTL_MODE_480I;
                    break;
                case OsdSize480p:
                    video_mode = DISPCTL_MODE_480P;
                    break;
		#ifdef SUPPORT_VGA
                case OsdSizeVga:
                	start_x += (WinStart_x-60);
                	start_y += (WinStart_y-30);	
                	width = 480 ;
                	height = 300 ;					
                    video_mode = DISPCTL_MODE_VGA;
                    break;	
		#endif
                case OsdSize576i:
                case OsdSize576iCVBS:					
                    start_y=89+48;
                    video_mode = DISPCTL_MODE_576I;
                    break;
                case OsdSize576p:
                	start_y=89+48;
                    video_mode = DISPCTL_MODE_576P;
                    break;
            #ifdef TV_MODE_720P
                case OsdSize720p:
                	start_x = 700;
                	start_y = 152;
                	width = 500 ;
                	height = 282 ;
                    video_mode = DISPCTL_MODE_720P;					
                    break;
            #endif
            #ifdef TV_MODE_1080P
                case OsdSize1080p:
                	start_x = 750;
                	start_y = 100;
                	width = 1000 ;
                	height = 635 ;
		#ifdef TV_MODE_1080I			
                    video_mode = DISPCTL_MODE_1080I;	
		#else
                    video_mode = DISPCTL_MODE_1080P;	
		#endif
                    break;
            #endif
                default:
		#ifdef WISTAR_VERSION
                    video_mode = DISPCTL_MODE_480P;					
		#else		
                    video_mode = DISPCTL_MODE_480I;					
		#endif
                    break;
            }			
            load_videoapp_data[5] |= 0x20000;
            load_videoapp_data[6] = start_y | (start_x<<16);
            load_videoapp_data[7] = width | (height<<16);
            load_videoapp_data[8] = preview_flag | (audio_mute<<1)|(video_mode<<24);
        }	
        else {
            preview_processing = 0;			
            // widesetting=NORMAL adjust=DISPLAY_ADJUST_KEEP_RATIO 
            // to get correct video aspect ratio
            load_videoapp_data[5] = (1<<2)|(DISPLAY_ADJUST_KEEP_RATIO<<3); 
            load_videoapp_data[6] = 0;
            load_videoapp_data[7] = 0;
            load_videoapp_data[8] = 0;
        }	

        pCmd->cmd_timeout_value[0] = 5; 

        AWGetAppID(media_type, &my_app_id) ; 
        pCmd->app_id = my_app_id;
        pCmd->ExecuteResultProcess = callback;
        AWAddCommandSet(pCmd);

        GPIO_UNMUTE;
        return 0;
    }

    return -1;
}

INT32S AWDefWinProc(PHWND sur_wnd, INT32U msg_type, INT32S msg_para)
{
	PHWND hwnd ;
	switch(msg_type)
	{
		case WM_KEYDOWN:
			INT32U *key_value = (INT32U *)msg_para ;
			switch(*key_value)
			{
				case UOP_POWER:
					AWCloseAllRunningApp() ;
					break ;
				default:
					break;
			}
			break ;
		case WM_DESTROY_CONTROL:
			hwnd = (PHWND)msg_para ;
			AWDestroyWinControl(hwnd, 1) ;
			break ;
	}
	return 1;
}

void AWRefreshSystemInfo(AWMSG *pMsg)
{
	return ;
}

void AWDeviceRemoveProcess(AWMSG *pMsg)
{
	return ;

}

void AWGUIKeyProcess(INT32U* iInputKey)
{
	PHWND iWinId = gpFocusWin ;
	INT32S	iProcResult ;
#ifdef USE_NEW_SCREEN_SAVER 
	if(gScreenSaveFlag && ((*iInputKey)!=0)) {
	    ScrSaverReset() ;
	}
#else
	if(gScreenSaveFlag&&((*iInputKey)!=0)&&(keytimer)&&(AVTimerIsEnabled(keytimer)))
	    AVTimerRestart(keytimer);
#endif

#ifdef ENABLE_USBGADGET
    if (PC_Connect)
        return;
#endif

#if HOTKEYENABLE
	if(GUIHotKeyProcess(&last_hotkey, iInputKey)){
		return ;
	}
#endif 

	while((iWinId)&&(iInputKey))//if have parent and key is not NULL
	{
		if(*iInputKey != UOP_NOP){
			key_input_timeout = 0;
		}
		iProcResult = 0 ;
		if(iWinId->WinProc)
			iProcResult = iWinId->WinProc(iWinId, WM_KEYDOWN, (INT32S)iInputKey);
		if(!iProcResult)
			iWinId->pClass->ClassProc(iWinId, WM_KEYDOWN, (INT32S)iInputKey);
		iWinId = iWinId->parent ;
	}
	return ;
}

#ifndef USE_NEW_SCREEN_SAVER 
static void key_timer_func(void *arg)
{
    screensaverstart();
}
#endif

void  AWGUINotificationProcess(AWMSG *pMsg)
{
	switch(pMsg->msg_id)
	{
		case NM_SYS_INFO_CHANGED:
			AWRefreshSystemInfo(pMsg) ;
			break;
		case NM_NEW_MEDIA_ADD:
			//AWAddNewSource(pMsg) ;
			break ;
		case NM_DEVICE_REMOVE:
			AWDeviceRemoveProcess(pMsg) ;
			break ;
		case NM_MEDIA_REMOVE:
			//AWSourceRemove(pMsg) ;
			break ;
		case NM_SYSTEM_READY:
			remote_io_init();
            AWSetHDScreen(1);
			InitialDevList(&DevList);
            if((GetDriverNum()==0))
                GUIShowWindow(LoadingWin);
			AutoRunMsg=pMsg;
	#ifdef SUPPORT_VGA
            if(CurrentOsdSize == OsdSizeVga)
                AWSetWinStart(WinStart_x,WinStart_y);
            else
                AWSetWinStart(0,0);
	#endif
	 if(gScreenSaveFlag)
            {
#ifdef USE_NEW_SCREEN_SAVER 
                ScrSaverSet(SCREEN_SAVER_ON) ;
                ScrSaverReset() ;
#else
                if (!keytimer) 
                    keytimer = AVTimerCreateFlag(OS_TICKS_PER_SEC*30, key_timer_func , NULL, AVTIMER_FLAG_PERIODIC); 
                else
                    AVTimerEnable(keytimer);
#endif
            }
            			
			break ;
#ifdef ENABLE_WATCHDOG
	case NM_SYS_RESTART_FINISH:
		Reset_Watchdog();
		break;
#endif
#if (defined ENABLE_BLUETOOTH)||(defined AML_DIR)		
	 	case NM_PLAY_GENERAL:
			
		if(CheckMediaPlaybackApp()){
//			CloseMediaPlaybackApp(0xff);
			check_app_flag = 1;
		}else{
			if ((WinTypeChoice == VIDEO)&& (NODIR_FLAG == 2) && (AWGetTopWindow(gpFocusWin) == AWFindWinCtrlByName(WinLogo))) 
//				run_video_speed_preview(preview_item_name,cur_level_index);
				run_video_preview();
			if((WinTypeChoice == AUDIO)&&(AWGetTopWindow(gpFocusWin) == AWFindWinCtrlByName(WinLogo))&&(NODIR_FLAG == 1))
				{
				DlyRunMusicPlay();
//				run_video_speed_preview(preview_item_name,cur_level_index);
				Music_Play_flag = 3 ;
				}
//			else
//	               PicPlayer(gCurOnlyFileName,NULL, NULL, 0);

		}
		break;	
#endif		
#ifdef ENABLE_NETWORK
		case NM_PREVIEW_READY:
			Preview_browser_task_post();
			break;
#endif
	}
	return ;
}


INT8U CheckMediaPlaybackVideoApp(void)
{
    INT8U my_app_id ;
     AWGetAppID(MEDIA_PIC, &my_app_id) ; 
    if (AWGetAppStatus(my_app_id) == APP_RUNNING)
        return PIC_APP_RUNNING;
    AWGetAppID(MEDIA_AVI, &my_app_id) ; 
    if (AWGetAppStatus(my_app_id) == APP_RUNNING)
        return VIDEO_APP_RUNNING;
    AWGetAppID(MEDIA_MPEG2, &my_app_id) ; 
    if (AWGetAppStatus(my_app_id) == APP_RUNNING)
        return VIDEO_APP_RUNNING;
    AWGetAppID(MEDIA_GENERAL, &my_app_id) ; 
    if (AWGetAppStatus(my_app_id) == APP_RUNNING)
        return VIDEO_APP_RUNNING;
    AWGetAppID(MEDIA_REAL, &my_app_id) ; 
    if (AWGetAppStatus(my_app_id) == APP_RUNNING)
        return VIDEO_APP_RUNNING;
#ifdef TXT_PLAYER 
	AWGetAppID(MEDIA_TXT, &my_app_id) ; 
	if(AWGetAppStatus(my_app_id) == APP_RUNNING)
        return VIDEO_APP_RUNNING;
#endif

    return 0;
}

static void AWGUIBroadCastProcess(AWMSG *pMsg)
{
	PHWND iWinId = gpFocusWin ;
	INT32S	iProcResult ;
	CHARSTR* unmountpath;
	CHARSTR* curpath;
	if(pMsg->msg_id == BM_TIMEOUT) {  
		 if(gScreenSaveFlag){		  
#ifdef USE_NEW_SCREEN_SAVER 
        if((scrsaver_on == SCREEN_SAVER_ON) && (!screensaverstatus())) {
            scrsaver_time++ ;
            if(scrsaver_time >= 3*60) {
                screensaverstart() ;
                ScrSaverReset() ;
            }
        }
        else {
            ScrSaverReset() ;
        }
#endif
				}
		if(iWinId)//if have parent and key is not NULL
		{
			iProcResult = 0 ;
        		if(iWinId->WinProc)
        			iProcResult = iWinId->WinProc(iWinId, WM_SECOND_TIMEOUT, 0);
        		if(!iProcResult)
        			iWinId->pClass->ClassProc(iWinId, WM_SECOND_TIMEOUT, 0);
		}
#ifndef AML_DIR
		if ((CheckMediaPlaybackVideoApp()==0) && (NODIR_FLAG == 2) && (AWGetTopWindow(gpFocusWin) == AWFindWinCtrlByName(WinLogo))) 
		{
			if(++key_input_timeout>3)
			{
				if(!user_power_flag)
				{
					if(unsupport_item_name != GetCurrVideoItemName() )
					{
						run_video_preview();
						key_input_timeout=0;
					}

				} // end of power_flag
			} // end of ++key
		}
		
		if((AWGetTopWindow(gpFocusWin) == AWFindWinCtrlByName(WinLogo))&&(NODIR_FLAG == 1)&&(Music_Play_flag == 2))
		{
				
			if(++Music_Play_Dlying > 1)
			{
				DlyRunMusicPlay();
				//Music_Play_flag = 3 ;
				Music_Play_flag = 1 ;
				Music_Play_Dlying = 0 ;
			}
		}
#endif	
		if( (AWGetTopWindow(gpFocusWin) == AWFindWinCtrlByName(WinLogo))&&(Check_string_is_roll() == UNROLL) ) 
		{
			if(++roll_string_timeout > 2)
			{
				#ifndef TV_MODE_1080P
                FileListViewStringRollDelaySecond();
                #endif
				roll_string_timeout = 0 ;
			}
		}

		if( (AWGetTopWindow(gpFocusWin) == AWFindWinCtrlByName(WinFileList))&&(Check_string_is_roll_file() == UNROLL) ) 
		{
			if(++roll_string_timeout > 2)
			{
				FileListViewStringRollDelaySecond_file();
				roll_string_timeout = 0 ;
			}
		}

		if (GetForceCloseMediaApp())
		{
			if (CheckMediaPlaybackVideoApp() == 0)
			{
				CleanForceCloseMediaApp();
				if (PC_Connect)
				{
					GUIShowWindow(WinPeripheralDev);
				}
			}

		}

		if((CheckMediaPlaybackVideoApp() != 0)&&(!GPIO_MUTE_STATUS)&&isMute	&&(PlayBack_unMute_ShowFlag == 1))
		{
			if(++PlayBack_unMute_waitting>PlayBack_unMute_WaitFor)
			{
				HideWinMoviePlayBackControl();
				PlayBack_unMute_ShowFlag = 0 ;
			}
		}
#ifdef AUTO_PLAY		
		if(multi_vol_autorun)
		{
			multi_vol_autorun = 0;
			MultiVolumeAutoRun();				
		}
		if(autorun_audio_flag)
		{
	    		if (CheckMediaPlaybackApp()==0)
			{
				autorun_audio_flag = 0;
				run_audio_only(CurDriver);
					
			}
		}
		if(autorun_pic_flag)
		{
	    		if (CheckMediaPlaybackVideoApp()==0)
			{
				autorun_pic_flag = 0;
				run_pic_only(CurDriver);
					
			}
		}
		if(autorun_video_flag)
		{
	    		if (CheckMediaPlaybackVideoApp()==0)
			{
				autorun_video_flag = 0;
				preview_video = 0;
				run_video_only(CurDriver);
			}
		}
#endif
#ifdef ENABLE_BLUETOOTH||AML_DIR
		if(check_app_flag){
		if(!CheckMediaPlaybackApp()){
			AWGUISendNMMessage(MEDIA_GUI, NM_PLAY_GENERAL, (INT32U)0);
			check_app_flag = 0;
		}
	    }
#endif		
	}
	if(EnableLogo==0)
		ReloadLogo();
	if(user_power_flag)
	{
		if(!CheckMediaPlaybackVideoApp())
		{
//			user_power_flag = 0;
			disable_video_layer();
#ifdef AML_NIKE
			set_chip_clocks(CHIP_CLOCK_0480);
#else			
			set_chip_clocks(CHIP_CLOCK_0360);
#endif			
			chip_power_on(0);
			READ_MPEG_REG_ISA(0x12000b4);
			WRITE_MPEG_REG_ISA(0x1102df8,0xfff);
			
		}
	}
		
	return ;
}


void AWGUIMsgProcess(AWMSG *pMsg)
{
	switch(pMsg->msg_type)
	{
		case MSG_INPUT_EVENT:
			if(pMsg->msg_id == WM_KEYDOWN) {
			    AWGUIKeyProcess(&pMsg->msg_value) ;
			}
			break ;
		case MSG_NOTIFICATION:
			AWGUINotificationProcess(pMsg) ;
			break ;
		case MSG_BROADCAST:
		    AWGUIBroadCastProcess(pMsg) ;
		    break ;
		default:
			break ;
	}
	
	return ;
}

INT32S AWGUISendNMMessage(INT32U media_type, INT16U msg_id, INT32U msg_value)
{
    INT8U iQError ;
    AWMSG *pMsg ;
    INT8U app_id ;
    INT8U gui_id;

	AWGetAppID(media_type, &app_id) ; 
	AWGetAppID(MEDIA_GUI, &gui_id) ; 

	pMsg = AWMsgMalloc() ;
	if(pMsg == NULL){
		WARNNING("AWAppTimerTimeout :: Not enough memory..") ;
		return -1 ;
	}
	pMsg->app_id = app_id ;
	pMsg->msg_type = MSG_NOTIFICATION ;
	pMsg->msg_id = msg_id ;
	pMsg->msg_value = msg_value ;

	iQError = AVQPost(AWGetAppMsgQueue(gui_id) , (void *)pMsg) ;
	if(iQError != OS_NO_ERR) //send msg fail
	{
		AWMsgFree(pMsg);
		ASSERT((1),"AWSystemReady: System Ready msg cannot send out...") ;
		return -1 ;
	}
}

INT8U NoVolumeControlWin()
{
//extern INT8U WinTypeChoice;	

	if(AWGetTopWindow(gpFocusWin) == AWFindWinCtrlByName(WinFileTypeSelect))
		return 1;
	if(AWGetTopWindow(gpFocusWin) == AWFindWinCtrlByName(WinDeviceSelect))
		return 1;
	if(AWGetTopWindow(gpFocusWin) == AWFindWinCtrlByName(WinSetup_net_menu))
		return 1;
	if((AWGetTopWindow(gpFocusWin) == AWFindWinCtrlByName(WinLogo))&&(WinTypeChoice == VIDEO))
		return 1;		
		
	return 0 ;
}

INT8U IsWingpFocusWin() // these wins have deal with Volume up and down
{
	if(AWGetTopWindow(gpFocusWin) == AWFindWinCtrlByName(WinFileList))
		return 1;
	if(AWGetTopWindow(gpFocusWin) == AWFindWinCtrlByName(WinLogo))
		return 1;		
	
	return 0;		
}

void run_audio_only(char *MountPath)
{	

	Playlist_t *Audioplaylist=NULL;
	
	unsigned app_type;
	int ret = -1;	
	
	
	Audioplaylist = fs_playlist_create((unsigned char *)MountPath,AW_AUDIO_LIST & NAV_PATH_TYPE , PLAYLIST_TYPE_AUDIO,0, DIR_SEARCH_DEPTH|0x001, &app_type);
	if(Audioplaylist) 
	{   			
		NODIR_FLAG = 1;
		WinTypeChoice = AUDIO ;

		Music_Play_flag = 1 ;	
			
		if(loading_autorun_flag){
			GUIShowWindow(WinLogo);
//			loading_autorun_flag = 0;
		}
		else{
			PHWND hwnd = AWFindWinCtrlByName(FileListView);
			if(hwnd){
				keep_current_index = 0 ;
				AWShowWinControl(hwnd,0);
				AWSetFocus(hwnd);
			}
		}
		ret = RunGPlayer(Audioplaylist);
		if(ret) 
		  {
			AWPlaylistDelete2(Audioplaylist);
		  }
	}
}

void run_video_only(char *MountPath)
{
	Playlist_t *Videoplaylist=NULL;
	
	unsigned app_type;
	int ret = -1;
	
	
	Videoplaylist = fs_playlist_create((unsigned char *)MountPath, AW_VIDEO_LIST&NAV_PATH_TYPE,PLAYLIST_TYPE_VIDEO,0,  DIR_SEARCH_DEPTH|0x001,&app_type);
	
	if(Videoplaylist){			

		NODIR_FLAG = 2;
		WinTypeChoice = VIDEO ;

												
		switch(app_type){
			
			case MEDIA_AVI:
				ret = RunAVIPlayer(Videoplaylist); 
				if(ret)
					AWPlaylistDelete2(Videoplaylist);
			break;

		#ifdef SUPPORT_RM
			case MEDIA_REAL:
				ret = RunRMPlayer(Videoplaylist); 
				if(ret)
					AWPlaylistDelete2(Videoplaylist);
			break;
        #endif
        
			case  MEDIA_MPEG2:
				ret = RunVOBPlayer(Videoplaylist); 
				if(ret)
					AWPlaylistDelete2(Videoplaylist);
			break;
			
			case  MEDIA_GENERAL:
			case  MEDIA_MOV:
				ret = RunGPlayer(Videoplaylist); 
				if(ret)
					AWPlaylistDelete2(Videoplaylist);
			break;
			
			default:
			break;
		}
	}  
//	loading_autorun_flag = 0;	    
}


void run_pic_only(char *MountPath)
{	
	Playlist_t *Picplaylist=NULL;
	
	INT32U app_type;
	int ret = -1;

	Picplaylist = fs_playlist_create((unsigned char *)MountPath, MEDIA_PIC&NAV_PATH_TYPE,PLAYLIST_TYPE_IMAGE,0,  DIR_SEARCH_DEPTH|0x001,&app_type);

	if(Picplaylist){
		
		//NODIR_FLAG = 0;
		ShowSixPicFlag = 0;
		MultiMenuToSingle = 3 ;
		//aw_title_mode = 0;
		PicParaTrans();
	   	ret = RunPICPlayer(Picplaylist,(unsigned char *)Logo_keyboard, sizeof(Logo_keyboard), 0); 
		if(ret)
			AWPlaylistDelete2(Picplaylist);
	}	
//	loading_autorun_flag = 0;	   
}
#ifdef MALATA_VERSION	
void auto_run_function(char *MountPath)
{
//   	loading_autorun_flag = 1;
	if((!check_media_file_exist(MountPath,(AW_AUDIO_LIST | AW_VIDEO_LIST),0x300|0x1))){//the high 8 bits is the depth to search     
		
		run_pic_only(MountPath);
		
		return;
    }
    else{	    		
		 if(check_media_file_exist(MountPath,AW_VIDEO_LIST,0x300|0x1)){
		
			run_video_only(MountPath);
			return;
		 }
		  else 
			if((check_media_file_exist(MountPath,AW_AUDIO_LIST,0x300|0x1))){
				ReloadLogo();
				run_audio_only(MountPath);
			    return;
			}
	}
}
#else
void auto_run_function(char *MountPath)
{
//   	loading_autorun_flag = 1;
	if((!check_media_file_exist(MountPath,(MEDIA_PIC | AW_VIDEO_LIST),0x300|0x1))){//the high 8 bits is the depth to search     
		
		run_audio_only(MountPath);
		return;
    }
    else{	    		
		 if(check_media_file_exist(MountPath,MEDIA_PIC,0x300|0x1)){
		
			run_pic_only(MountPath);
			return;
		 }
		  else 
			if((check_media_file_exist(MountPath,AW_VIDEO_LIST,0x300|0x1))){

				run_video_only(MountPath);
			    return;
			}
	}
}

#endif

INT8U ComboPlayMP3_CurDriver(void)
{
    Playlist_t *Audioplaylist=NULL;
    INT32U combo_type;
       //comboplayflag=1;
    //unsigned char *Path_String = "/mnt/D";
    //if (Get_SlideShow_Flag() == 0)
    Audioplaylist=fs_playlist_create((unsigned char *)CurDriver,AW_AUDIO_LIST & NAV_PATH_TYPE , PLAYLIST_TYPE_AUDIO, 0,AUTORUN_DEPTH|0x1,&combo_type);
    if (Audioplaylist) 
    {
        Music_Pic_mix_flag=1;		
        if (RunGPlayer_loop(Audioplaylist))
         AWPlaylistDelete2(Audioplaylist);
     return 1;
    }	
    else
    return 0;
}

#if HOTKEYENABLE
INT8U GUIHotKeyProcess(INT32U* last_hotkey, INT32U* iInputKey)
{
	INT8U my_app_id ;
	PHWND top = AWGetTopWindow(gpFocusWin);
#ifndef TXT_PLAYER 
	AWGetAppID(MEDIA_TXT, &my_app_id) ; 
	if(AWGetAppStatus(my_app_id) == APP_RUNNING){
		return 0 ;
	}
#else
    extern char txt_read_finish;
    if(txt_read_finish == FALSE)
    {
        return 0;
    }
#endif
    if((copy_del_flag == SYS_DEL_FILE)||(copy_del_flag == SYS_COPY_FILE))
        return 0;

	switch(*iInputKey){
		case UOP_SETUP:
			if(AWGetTopWindow(gpFocusWin) != AWFindWinCtrlByName(WinSetup_net_menu)){
				if(CheckMediaPlaybackVideoApp()){
					hotkey_flag = 1 ;
					CloseMediaPlaybackApp(0xff);
					*last_hotkey = UOP_SETUP ;
				}
				else {
                	AWHideWindow(top);
					if(filelist_show_flag == 1){
					filelist_show_flag = 0;
					GUIHideWindow(WinFileList);
					}
					force_display_logo = 1 ;
					ReloadLogo(); 
					GUIShowWindow(WinSetup_net_menu);
				}
				return 1 ;
			}
			break ;

		
		case UOP_SLIDE_SHOW: // UOP_6
            if(GetDriverNum()==0)
            {
                *last_hotkey = UOP_NOP ;
                return 1;
            } 
        #ifdef FILE_SORTING
            FileNavSetDefaultSortType(FILENAV_SORT_NONE, FILENAV_SORT_NUM);
        #endif
			if( (AWGetTopWindow(gpFocusWin) != AWFindWinCtrlByName(WinPicInfoBar))
				&&(check_media_file_exist(CurDriver,MEDIA_PIC,0x300|0x1)==1) 
			  ){
			  	cur_filetype_flag = 2;
			  	if(CheckMediaPlaybackVideoApp()){
				  	hotkey_flag = 1 ;
				  	CloseMediaPlaybackApp(0xff);
					*last_hotkey = UOP_SLIDE_SHOW ;
				}
				else {
					NODIR_FLAG = 0;
					SetPicRunStatus(0);
					ShowSixPicFlag=0;
                    WinTypeChoice = PHOTO ;
					AWHideWindow(AWGetTopWindow(gpFocusWin));
					MultiMenuToSingle=3; 
					display_multi_pic();
				}
				return 1 ;	
			}else{
					*iInputKey = UOP_NOP;
			}
			break;
			
		case UOP_MUSIC: //UOP_7
            if(GetDriverNum()==0)
            {
                *last_hotkey = UOP_NOP ;
                return 1;
            } 
#ifndef AML_DIR
			if(AWGetTopWindow(gpFocusWin) == AWFindWinCtrlByName(WinLogo)){
			#ifdef TXT_PLAYER
				if(((WinTypeChoice == VIDEO)||((WinTypeChoice == TXT)))&&(JudgeCurrDirHaveMediaOrNull(AUDIO) != 0))
			#else
				if((WinTypeChoice == VIDEO)&&(JudgeCurrDirHaveMediaOrNull(AUDIO) != 0))
			#endif
				{
					cur_filetype_flag = 1;
					if(CheckMediaPlaybackVideoApp()){
						hotkey_flag = 1 ;
						CloseMediaPlaybackApp(0xff);
						*last_hotkey = UOP_MUSIC ;
						return 1 ;
					}
					else if(AutoRunWhenSelectAudioOrVideo(AUDIO)){
						AWHideWindow(AWGetTopWindow(gpFocusWin));
						force_display_logo = 1 ;
						ReloadLogo(); 
						GUIShowWindow(WinLogo);
						return 1 ;
					}
				}
			}
			else if(JudgeCurrDirHaveMediaOrNull(AUDIO) != 0){
				cur_filetype_flag = 1;
				if(CheckMediaPlaybackVideoApp()){
					hotkey_flag = 1 ;
					CloseMediaPlaybackApp(0xff);
					*last_hotkey = UOP_MUSIC ;
					return 1 ;
				}
				else if(AutoRunWhenSelectAudioOrVideo(AUDIO)){
					AWHideWindow(AWGetTopWindow(gpFocusWin));
					GUIShowWindow(WinLogo);
					return 1 ;
				}
			}
				
			break;
#else
			if(AWGetTopWindow(gpFocusWin) == AWFindWinCtrlByName(WinLogo)){
				if(WinTypeChoice == VIDEO){
					cur_filetype_flag = 1;
					if(CheckMediaPlaybackVideoApp()){
						hotkey_flag = 1 ;
						CloseMediaPlaybackApp(0xff);
						*last_hotkey = UOP_MUSIC ;
						return 1 ;
					}
					else {
                    	AWHideWindow(top);
						NODIR_FLAG = 1;
						FileListOnShowFlag = 0;
						WinTypeChoice = AUDIO ;
						ReloadLogo();
						GUIShowWindow(WinLogo);
						return 1 ;
					}
				}
			}
			else{
				cur_filetype_flag = 1;
				if(CheckMediaPlaybackVideoApp()){
					hotkey_flag = 1 ;
					CloseMediaPlaybackApp(0xff);
					*last_hotkey = UOP_MUSIC ;
					return 1 ;
				}
				else{
                	AWHideWindow(top);
					ReloadLogo();
					NODIR_FLAG = 1;
				    FileListOnShowFlag = 0;
					WinTypeChoice = AUDIO ;
					GUIShowWindow(WinLogo);
					return 1 ;
				}
			}
				
			break;
#endif
		case UOP_MOVIE: // UOP_9
            if(GetDriverNum()==0)
            {
                *last_hotkey = UOP_NOP ;
                return 1;
            } 
    #ifdef AML_DIR
        #ifdef FILE_SORTING
            FileNavSetDefaultSortType(FILENAV_SORT_NONE, FILENAV_SORT_NUM);
        #endif
    #endif
			if(AWGetTopWindow(gpFocusWin) == AWFindWinCtrlByName(WinLogo)){
			#ifdef TXT_PLAYER
				if(((WinTypeChoice == AUDIO)||((WinTypeChoice == TXT)))&&(JudgeCurrDirHaveMediaOrNull(VIDEO) != 0))
			#else
				if((WinTypeChoice == AUDIO)&&(JudgeCurrDirHaveMediaOrNull(VIDEO) != 0))
			#endif
			    {
					cur_filetype_flag = 0;
					if(CheckMediaPlaybackVideoApp()){
						hotkey_flag = 1 ;
						CloseMediaPlaybackApp(0xff);
						*last_hotkey = UOP_MOVIE ;
						return 1 ;
					}
					else {
                        FileListOnShowFlag = 0;
                    	AWHideWindow(top);
						AutoRunWhenSelectAudioOrVideo(VIDEO);
						return 1 ;
					}
				}
			}
			else if(CheckMediaPlaybackVideoApp()&&(WinTypeChoice == VIDEO))
			{
                *last_hotkey = UOP_NOP ;
                return 1;
			}
			else if(JudgeCurrDirHaveMediaOrNull(VIDEO) != 0){
				cur_filetype_flag = 0;
				if(CheckMediaPlaybackVideoApp()){
					hotkey_flag = 1 ;
					CloseMediaPlaybackApp(0xff);
					*last_hotkey = UOP_MOVIE ;
					return 1 ;
				}
				else {
					preview_video = 0 ;
                    FileListOnShowFlag = 0;
                	AWHideWindow(top);
					AutoRunWhenSelectAudioOrVideo(VIDEO) ;
					return 1 ;
				}
			}
			break ;
		case UOP_PHOTO: // UOP_0
            if(GetDriverNum()==0)
            {
                *last_hotkey = UOP_NOP ;
                return 1;
            } 
        #ifdef FILE_SORTING
            FileNavSetDefaultSortType(FILENAV_SORT_NONE, FILENAV_SORT_NUM);
        #endif
			if( (AWGetTopWindow(gpFocusWin) != AWFindWinCtrlByName(Win15Pic))
				&&(check_media_file_exist(CurDriver,MEDIA_PIC,0x300|0x1)==1) )
			{
			  	if(CheckMediaPlaybackVideoApp()){
				  	hotkey_flag = 1 ;
				  	CloseMediaPlaybackApp(0xff);
					*last_hotkey = UOP_PHOTO ;
				}
				else {
					NODIR_FLAG = 0 ;
					set_current_logo_type(1); 
                    WinTypeChoice = PHOTO ;
					GUIShowWindow(Win15Pic);
				}
				return 1 ;	
			}else{
					*iInputKey = UOP_NOP;
			}
        break;
    #ifdef TXT_PLAYER
        case UOP_TXT:
            if(GetDriverNum()==0)
            {
                *last_hotkey = UOP_NOP ;
                return 1;
            } 
        #ifndef AML_DIR
			if(AWGetTopWindow(gpFocusWin) == AWFindWinCtrlByName(WinDebug)){
				if((WinTypeChoice != TXT)&&(JudgeCurrDirHaveMediaOrNull(TXT) != 0)){
					cur_filetype_flag = 3;
					if(CheckMediaPlaybackVideoApp()){
						hotkey_flag = 1 ;
						CloseMediaPlaybackApp(0xff);
						*last_hotkey = UOP_TXT ;
						return 1 ;
					}
					else if(AutoRunWhenSelectAudioOrVideo(TXT)){
						AWHideWindow(AWGetTopWindow(gpFocusWin));
						ReloadLogo(); 
						GUIShowWindow(WinLogo);
						return 1 ;
					}
				}
			}
			else if(JudgeCurrDirHaveMediaOrNull(TXT) != 0){
				cur_filetype_flag = 3;
				if(CheckMediaPlaybackVideoApp()){
					hotkey_flag = 1 ;
					CloseMediaPlaybackApp(0xff);
					*last_hotkey = UOP_TXT ;
					return 1 ;
				}
				else if(AutoRunWhenSelectAudioOrVideo(TXT))
				{
					AWHideWindow(AWGetTopWindow(gpFocusWin));
					GUIShowWindow(WinLogo);
					return 1 ;
				}
			}
				
			break;
        #else
			if(AWGetTopWindow(gpFocusWin) == AWFindWinCtrlByName(WinFileList)){
				if(WinTypeChoice != TXT){
					cur_filetype_flag = 3;
					if(CheckMediaPlaybackVideoApp()){
						hotkey_flag = 1 ;
						CloseMediaPlaybackApp(0xff);
						*last_hotkey = UOP_TXT ;
						return 1 ;
					}
					else {
                    	AWHideWindow(top);
						NODIR_FLAG = 1;
				        FileListOnShowFlag = 0;
						WinTypeChoice = TXT ;
                        gGetFileType = MEDIA_TXT | NAV_IS_DIR;
						ReloadLogo();
						GUIShowWindow(WinFileList);
						return 1 ;
					}
				}
			}
			else if(AWGetTopWindow(gpFocusWin) == AWFindWinCtrlByName(WinDebug))
			{
			    if(WinTypeChoice == TXT)
			    {
                    *last_hotkey = UOP_NOP ;
                    return 1;
                }
			}
			else{
				cur_filetype_flag = 3;
				if(CheckMediaPlaybackVideoApp()){
					hotkey_flag = 1 ;
					CloseMediaPlaybackApp(0xff);
					*last_hotkey = UOP_TXT ;
					return 1 ;
				}
				else{
                	AWHideWindow(top);
					ReloadLogo();
					NODIR_FLAG = 1;
				    FileListOnShowFlag = 0;
					WinTypeChoice = TXT ;
                    gGetFileType = MEDIA_TXT | NAV_IS_DIR;
					GUIShowWindow(WinFileList);
					return 1 ;
				}
			}
            break ;
        #endif
    #endif
		default:
			break ;
	}
	
	return 0 ;
}

INT32U APPHotKeyProcess(INT32U* last_hotkey)
{
	PHWND top = AWGetTopWindow(gpFocusWin);

	switch(*last_hotkey)
    {
        case UOP_SETUP:
            GUIShowWindow(WinSetup_net_menu);
            *last_hotkey = UOP_NOP ;
            break ;

        case UOP_SLIDE_SHOW: // UOP_6
            NODIR_FLAG = 0;
            SetPicRunStatus(0);
            ShowSixPicFlag=0;
            WinTypeChoice = PHOTO ;
            AWHideWindow(top);
            MultiMenuToSingle=3; 
            display_multi_pic();
            *last_hotkey = UOP_NOP ;
            break ;
			
        case UOP_MUSIC: //UOP_7
        #ifndef AML_DIR
            AutoRunWhenSelectAudioOrVideo(AUDIO);
        #else				
            NODIR_FLAG = 1;
            WinTypeChoice = AUDIO ;
            FileListOnShowFlag = 0;
            //ReloadLogo();
        #endif
            AWHideWindow(top);
            GUIShowWindow(WinLogo);
            *last_hotkey = UOP_NOP ;
            break ;
        case UOP_MOVIE:     //UOP_9
            FileListOnShowFlag = 0;
            AWHideWindow(top);
            AutoRunWhenSelectAudioOrVideo(VIDEO);
            *last_hotkey = UOP_NOP ;
            break ;
			
        case UOP_PHOTO:    //UOP_0
            NODIR_FLAG = 0;
            WinTypeChoice = PHOTO ;
            set_current_logo_type(1); 
            GUIShowWindow(Win15Pic);
            *last_hotkey = UOP_NOP ;
            break ;
    #ifdef TXT_PLAYER
        case UOP_TXT:
        #ifndef AML_DIR
            NODIR_FLAG = 3;					
            WinTypeChoice = TXT;
            AutoRunWhenSelectAudioOrVideo(TXT);
        #else
            NODIR_FLAG = 0;					
            WinTypeChoice = TXT;
            gGetFileType = MEDIA_TXT | NAV_IS_DIR;
            FileListOnShowFlag = 0;
            AWHideWindow(top);
            GUIShowWindow(WinFileList);
       #endif
            *last_hotkey = UOP_NOP ;
            break ;
    #endif
			
        default :
            break ;
    }
	return 0 ;
}
#endif 
